//
//  ProjectInfoViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/6.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "ProjectInfoViewController.h"
#import "YSPMSInfoDetailHeaderCell.h"
#import "YSNetManager.h"
#import "DataManager.h"
#import "ProjectInfoModel.h"
#import "ProjectInfoDetailViewController.h"
#import "ProjectUnitInfoViewController.h"
#import "ProjectPeopleInfoViewController.h"
#import "ProjectFinanceInfoViewController.h"

@interface ProjectInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *dataSourceArray;
@property (nonatomic, copy) NSArray *sourceArray;
@property (nonatomic, strong) NSMutableArray *sourceArr;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *projectName;
@end

@implementation ProjectInfoViewController
- (NSMutableArray *)sourceArr {
    if(!_sourceArr){
        _sourceArr = [NSMutableArray array];
    }
    return _sourceArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title =  @"项目信息详情";
    self.titleArray = @[@"项目编号",@"工程地址",@"项目线条",@"项目所属年份",@"项目性质",@"合同价（万元）",@"项目类型",@"项目经理",@"所属部门",@"工期（天）",@"项目状态"];
    self.dataSourceArray = @[@"信息详情", @"单位信息", @"人员信息", @"财务信息"];
    [self initUI];
    [self doNetworking];
}
- (void)initUI {
    self.tableView = [[UITableView alloc]initWithFrame:CGRects(0, 0, 375, 635) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@/%@",YSDomain, getBaseInfoDeatilApp, self.projectId];
    [YSNetManager ys_request_GETWithUrlString:urlString isNeedCache:NO parameters:nil successBlock:^(id response) {
        NSLog(@"成功%@",response);
        if([response[@"code"]  isEqual: @1]){
            self.sourceArray = [DataManager getPMSInfoData:response];
            NSLog(@"====%@",self.sourceArray);
            [self dealWithData];
            self.projectName = response[@"data"][@"name"];
            [self.tableView reloadData];
            
        }else{
            [QMUITips showInfo:response[@"msg"] inView:self.view hideAfterDelay:1];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败%@",error);
    } progress:nil];
}
- (void)dealWithData {
    ProjectInfoModel *model = self.sourceArray[0];

    [self.sourceArr addObject:model.code?model.code:@"-"];
    [self.sourceArr addObject:[NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.address]];
    [self.sourceArr addObject:model.itemLineName?model.itemLineName:@"-"];
    [self.sourceArr addObject:model.belongTime?model.belongTime:@"-"];
    
    [self.sourceArr addObject:model.proNatureName?model.proNatureName:@"-"];
    [self.sourceArr addObject:@"20"];
    [self.sourceArr addObject:model.proTypeStr?model.proTypeStr:@"-"];
    [self.sourceArr addObject:model.proManName?model.proManName:@"-"];
    [self.sourceArr addObject:model.deptStr?model.deptStr:@"-"];
    [self.sourceArr addObject:model.timeLimit?model.timeLimit:@"-"];
    [self.sourceArr addObject:model.proStatusName?model.proStatusName:@"无"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return self.titleArray.count+1;
    }else{
        return self.dataSourceArray.count;
    }
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200*kHeightScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section == 0 && indexPath.row == 0){
        return 80*kHeightScale;
    }else{
        return UITableViewAutomaticDimension;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YSPMSInfoDetailHeaderCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[YSPMSInfoDetailHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PMSInfoDetailHeaderCell"];
        }
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.separatorInset = UIEdgeInsetsMake(0, kSCREEN_WIDTH, 0, 0);
        if (indexPath.row == 0) {
            UILabel *projectLabel = [[UILabel alloc]init];
            projectLabel.textAlignment = NSTextAlignmentCenter;
            projectLabel.font = [UIFont systemFontOfSize:17];
            projectLabel.textColor = [UIColor colorWithHue:51.0/255.0 saturation:51.0/255.0 brightness:51.0/255.0 alpha:1.0];
            projectLabel.numberOfLines = 0;
            projectLabel.adjustsFontSizeToFitWidth = YES;
            projectLabel.text = self.projectName;
            [cell addSubview:projectLabel];
            [projectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(cell);
                make.size.mas_equalTo(CGSizeMake(kSCREEN_WIDTH-80*kWidthScale, 80*kHeightScale));
            }];
        }else {
            cell.titleLabel.text = self.titleArray[indexPath.row-1];
            if(self.sourceArr.count > 0){
                cell.contentLabel.text =self.sourceArr[indexPath.row-1];
            }
        }
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = self.dataSourceArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 1){
        if(indexPath.row == 0){
            ProjectInfoDetailViewController *infoDetail = [[ProjectInfoDetailViewController alloc]init];
            infoDetail.projectId = self.projectId;
            infoDetail.model = self.sourceArray[0];
            [self.navigationController pushViewController:infoDetail animated:YES];
        }else if(indexPath.row == 1){
            ProjectUnitInfoViewController *unitInfo = [[ProjectUnitInfoViewController alloc]init];
            unitInfo.projectId = self.projectId;
            [self.navigationController pushViewController:unitInfo animated:YES];
        }else if(indexPath.row == 2){
            ProjectPeopleInfoViewController *peopleInfo = [[ProjectPeopleInfoViewController alloc]init];
            peopleInfo.projectId = self.projectId;
            [self.navigationController pushViewController:peopleInfo animated:YES];
        }else {
            ProjectFinanceInfoViewController *financeInfo = [[ProjectFinanceInfoViewController alloc]init];
            financeInfo.projectId = self.projectId;
            [self.navigationController pushViewController:financeInfo animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
